#include "SDLWindow.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>


int sdl_main_loop(void *window);

extern int SDL_RecreateWindow(SDL_Window *window, Uint32 flags);
SDLWindow::SDLWindow()
{
    mWindowID = 0;
    mWidth    = 0;
    mHeight   = 0;
}

SDLWindow::~SDLWindow()
{

}

void SDLWindow::render()
{
    //Clear screen
    /* SDL_SetRenderDrawColor(mRenderer, 0x00, 0x55, 0xFF, 0xFF); */
    /* SDL_RenderClear(mRenderer); */

    /* //Update screen */
    /* SDL_RenderPresent(mRenderer); */

    //Clear color buffer
    glClear(GL_COLOR_BUFFER_BIT);

    //Render quad
    glBegin(GL_QUADS);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f(0.5f, -0.5f);
    glVertex2f(0.5f, 0.5f);
    glVertex2f(-0.5f, 0.5f);
    glEnd();

    glBegin(GL_LINES);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f(0.5f, -0.7f);
    glEnd();

    SDL_GL_SwapWindow(mWindow);
}

bool SDLWindow::init(const void *winID)
{
#if 0
    int i =  SDL_GetNumRenderDrivers();
    mWindow = SDL_CreateWindowFrom(winID);

    if (!mWindow)
    {
        printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
        return false;
    }

    /* mWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,  480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE ); */
    /* mWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,  480, SDL_WINDOW_BORDERLESS); */
    /* if ( !mWindow ) */
    /* { */
    /*     printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); */
    /*     return false; */
    /* } */

    //Set texture filtering to linear
    if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
    {
        printf("Warning: Linear texture filtering not enabled!");
    }

    //Create renderer for window
    //mRenderer = SDL_CreateRenderer( mWindow, 1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC );
    mRenderer = SDL_CreateRenderer(mWindow, 1, SDL_RENDERER_ACCELERATED);

    /* mRenderer = SDL_CreateRenderer(mWindow, 1, SDL_RENDERER_SOFTWARE); */
    if (mRenderer == NULL)
    {
        printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
        SDL_DestroyWindow(mWindow);
        mWindow = NULL;
    }
    else
    {
        //Initialize renderer color
        SDL_SetRenderDrawColor(mRenderer, 0x00, 0xFF, 0xFF, 0xFF);

        //Grab window identifier
        mWindowID = SDL_GetWindowID(mWindow);

        //Flag as opened
    }

#endif

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

    //Create window
    mWindow = SDL_CreateWindowFrom(winID);
    /* SDL_RecreateWindow(mWindow, SDL_WINDOW_OPENGL); */
    /* mWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,  480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); */
    if (!mWindow)
    {
        printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
        return false;
    }
    else
    {
        //Create context

    }


    SDL_CreateThread(sdl_main_loop, "test", (void *)this);

    return true;
}

int sdl_main_loop(void *window)
{
    SDLWindow *sdlWin = (SDLWindow *)window;
    int cnt = 25 * 10;

    sdlWin->mContext = SDL_GL_CreateContext(sdlWin->mWindow);
    if (sdlWin->mContext == NULL)
    {
        printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
    }
    else
    {
        //Use Vsync
        if (SDL_GL_SetSwapInterval(1) < 0)
        {
            printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
        }

        //Initialize OpenGL
        if (!sdlWin->initGL())
        {
            printf("Unable to initialize OpenGL!\n");
        }
    }

    SDL_StartTextInput();

    while (1)
    {
        sdlWin->render();
        SDL_Delay(40);
    }
}

bool SDLWindow::initGL()
{
    bool success = true;
    GLenum error = GL_NO_ERROR;

    //Initialize Projection Matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //Check for error
    error = glGetError();

    if (error != GL_NO_ERROR)
    {
        printf("Error initializing OpenGL! %s\n", gluErrorString(error));
        success = false;
    }

    //Initialize Modelview Matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //Check for error
    error = glGetError();

    if (error != GL_NO_ERROR)
    {
        printf("Error initializing OpenGL! %s\n", gluErrorString(error));
        success = false;
    }

    //Initialize clear color
    glClearColor(0.f, 0.f, 0.f, 1.f);

    //Check for error
    error = glGetError();

    if (error != GL_NO_ERROR)
    {
        printf("Error initializing OpenGL! %s\n", gluErrorString(error));
        success = false;
    }

    return success;
}
